<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
webUtils.nameSpace("wiseMen.platform");
wiseMen.platform.optBtnGrid=function(cfg){
	cfg=cfg||{};
	var me=this;
	me.grid=cfg.gridDom.bootstrapTable({
    	url:ctx + "/optBtnAction/queryInFuncPoint.do",
    	queryParams:function(d){
    		if(me.grid){
    			d.funcPointId=me.grid.funcPointId;
    		}
			
			return d;
    	},
    	columns:[
				{field: '_id',checkbox:true},
                {title: '按钮名称', field: 'title', width: "80"},
                {title: '类型', field: 'btnType',
                	formatter:function(value,row, index){
                		return webUtils.getDataDicItemVal("BTN_TYPE",value).name;
                	}},
                {title: '权限编码', field: 'optAuthCode'},
                {title: '执行脚本', field: 'exeScript'},
                {title: '排序', field: 'indexValue', width: "80",
                	formatter:function(value,row, index){
                		var html='<a class="btn btn-link btn-sm _j_upBt" data-id="'+row.id+'" data-index="'+value+'">'
                		+'<span class="glyphicon glyphicon-chevron-up"></span>'
                		+'</a>'
                		+'<a class="btn btn-link btn-sm _j_downBt"  data-id="'+row.id+'" data-index="'+value+'">'
                		+'<span class="glyphicon glyphicon glyphicon-chevron-down"></span>'
                		+'</a>';
                		return html;
                	}}
    	],
    	onLoadSuccess:function(data ){
    		cfg.gridDom.find("._j_upBt").click(function () {
                _this = $(this);
                var id=_this.data("id");
                var index=_this.data("index");
                sort(id,index,"goUp");
            });
    		cfg.gridDom.find("._j_downBt").click(function () {
                _this = $(this);
                var id=_this.data("id");
                var index=_this.data("index");
                sort(id,index,"goDown");
            });
    	}
    });
	
	function sort(id,index,action){
		$.post(ctx+"/optBtnAction/sort.action",
				{id:id,action:action,index:index},
				function(data){
					if(data.success){
						me.grid.bootstrapTable("refresh");
					}else{
						webUtils.alert(data.msg);
					}
				},
				"json"
			);
	}
	
	if(cfg.toolbarSelector){
		var toolbar=$(cfg.toolbarSelector);
		toolbar.find(".batAddBt").click(function(){
			if (!me.grid.funcPointId) {
                webUtils.alert("请选择具体的功能点");
            }else{
            	var sub = top.webUtils.topicSubscribe("optBtn.edit.success", function (args) {
                    dialog.close();
                    me.grid.bootstrapTable("refresh");
                });
            	
            	var dialog=webUtils.dialog({
                 	 title:'批量添加按钮',
                 	 url:ctx + '/optBtnAction/addByBatchView.do?funcPointId=' + me.grid.funcPointId,
                 	 onremove:function () {
                 		sub.remove();
                      }
                  });
            }
		});
		
		toolbar.find(".addBt").click(function(){
			if (!me.grid.funcPointId) {
                webUtils.alert("请选择具体的功能点");
                return false;
            }
            var sub = top.webUtils.topicSubscribe("optBtn.edit.success", function (args) {
            	dialog.close();
                me.grid.bootstrapTable("refresh");
            });
            var dialog = editOptBtn({
                funcPointId: me.grid.funcPointId,
                onClose: function () {
                    sub.remove();
                }
            });
		});
		
		toolbar.find(".editBt").click(function(){
			var rows=me.grid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var sub = top.webUtils.topicSubscribe("optBtn.edit.success", function (args) {
    				dialog.close();
                    me.grid.bootstrapTable("refresh");
                });
        		var row=rows[0];
        		var dialog = editOptBtn({
                    id: row.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
        	}else {
                webUtils.alert("请选择需要编辑的数据");
            }
		});
		
		toolbar.find(".delBt").click(function(){
			var rows=me.grid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var delOptAuth=false;
		            	if(window.confirm("请问需要同时删除操作权限吗？")){
		            		delOptAuth=true;
		            	}
		            	
		        		var selecteds = [];
		                $.each(rows, function (i, n) {
		                    selecteds.push(n.id);
		                });
		                $.post(ctx + "/optBtnAction/delByIds.do", {
		                	ids: selecteds.join(","),
		                    delOptAuth:delOptAuth
		                }, function (data) {
		                    if (data.success) {
		                    	me.grid.bootstrapTable("refresh");
		                    } else {
		                    	webUtils.alert(data.msg);
		                    }
		                }, 'json');
					}
        		});
        	} else {
        		webUtils.alert("请选择需要删除的数据");
            }
		});
		
		toolbar.find(".roleAuthBt").click(function(){
			var rows=me.grid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var row = rows[0];
        		$.getJSON(ctx+"/optAuthAction/loadRoles.do",{
            		id:row.optAuthId
            	},function(data){
            		data=data||{};
            		webUtils.roleSelector({
                		hasSelectedRoles:data,
                		onSelect:function(rows){
                			var roles=[];
                			$.each(rows,function(i,n){
                				roles.push(n.id);
                			});
               				$.post(ctx + "/optAuthAction/setRoles.do", {
                                    id:row.optAuthId,roleIds: roles.join(",")
                               }, function (data) {
                                   if (data.success) {
                                   	webUtils.alert("配置成功"); 
                                   } else {
                                   	webUtils.error(data.msg);
                                   }
                               }, 'json');
                		}
                	});
            	});
        	}
		}); 
		
		
		
	}
	
	
	
	
	
	var editOptBtn = function (params) {
        params = params || {};
        var t = [];
        if (params.id) {
            t.push("id=" + params.id);
        }
        if (params.funcPointId) {
            t.push("funcPointId=" + params.funcPointId);
        }
        
        var dialog=webUtils.dialog({
        	 title:'编辑操作按钮',
        	 url:ctx + '/optBtnAction/editView.do?' + t.join("&"),
        	 onremove:function () {
        		 if (params.onClose) {
                     params.onClose.call(null);
                 }
             }
         });
        
        return dialog;
    }
	
	return me.grid;
}

var setIndexValue=function(bt,id){
	var _$bt=$(bt);
	var indexValue=_$bt.html();
	var v=prompt("请输入顺序号",indexValue); 
	if(v!=null){
		$.post(ctx+"/optBtnAction/updateIndex.do",{id:id,indexValue:v},function(data){
			if(data.success){
				_$bt.html(v);
			}
		});
	}	
}
</script>